package org.lyq.erp.auth.role.controller;

import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.auth.menu.pojo.Menu;
import org.lyq.erp.auth.menu.service.MenuService;
import org.lyq.erp.auth.resources.pojo.Resources;
import org.lyq.erp.auth.resources.service.ResourcesService;
import org.lyq.erp.auth.role.pojo.Role;
import org.lyq.erp.auth.role.service.RoleService;
import org.lyq.erp.config.json.JSONResult;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/role" })
public class RoleController {
	
	private static Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Resource
	private RoleService roleService;

	@Resource
	private MenuService menuService;

	@Resource
	private ResourcesService resourcesService;

	@RequestMapping({ "/list" })
	public String list(ModelMap map) {
		getAllData(map);
		return "role_list";
	}

	@RequestMapping({ "/insertAndUpdate" })
	public String insertAndUpdate(Role role, String menuUuid, ModelMap map) {
		if (!insertValidation(role)) {
			role.popuperrorMessage = "true";
			map.put("dataBean", role);
			getAllData(map);
			return "role_list";
		}
		try {
			if (role.getRoleUuid() == null) {
				this.roleService.insert(role);
			} else {
				this.roleService.update(role);
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
			role.addErrorMessage("role004");
			role.popuperrorMessage = "true";
			map.put("dataBean", role);
			getAllData(map);
			return "role_list";
		}
		return "redirect:/role/list?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/getRole" })
	@ResponseBody
	public JSONResult getRole(Integer roleUuid) {
		if (roleUuid != null) {
			Role role = this.roleService.getRoleByRoleUuid(roleUuid);
			if (role != null) {
				return JSONResult.ok(role);
			}
			return JSONResult.errorMsg("role003");
		}

		return JSONResult.errorMsg("role003");
	}

	@RequestMapping({ "/delete" })
	public String delete(Role role, String menuUuid, ModelMap map) {
		if (role.getRoleUuid() != null) {
			try {
				this.roleService.delete(role.getRoleUuid());
				return "redirect:/role/list?menuUuid=" + menuUuid;
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}

		role.addErrorMessage("role005");
		map.put("dataBean", role);
		getAllData(map);
		return "role_list";
	}

	private boolean insertValidation(Role role) {
		if (ObjectEmptyUtils.isEmptyString(role.getRoleName())) {
			role.addErrorMessage("roleName", "role001");
		}
		if (ObjectEmptyUtils.isEmptyString(role.getRoleCode())) {
			role.addErrorMessage("roleCode", "role002");
		}

		return role.getMap().isEmpty();
	}

	private void getAllData(ModelMap map) {
		List<Role> rList = this.roleService.selectAllRole();
		map.put("rList", rList);

		List<Menu> menuList = this.menuService.selectAllMenu();
		map.put("menuList", menuList);

		List<Resources> resList = this.resourcesService.selectALlResources();
		map.put("resList", resList);
	}
}
